<template>
	<view class="video-detail-wrapper full box relative flex column" :style="{'--padding':statusBarHeight_}">
		<view class="fixed-header">
			<image @click="backHandle" src="../static/chevron-left-white.png" mode="aspectFill" class="chevron-icon">
			</image>
		</view>
		<view class="relative flex-1 box w-100">
			<view class="absolute inset">
				<view class="full box relative">
					<video :enable-progress-gesture="true" :initial-time="0" @timeupdate="onTimeUpdate" loop
						:muted="isMuted" id="videoRef" :controls="true" class="full video-target" :src="videoPath"
						object-fit="contain" @loadedmetadata="videoLoadEnd" autoplay @click="handleVideoClick" />
					<!-- <cover-image v-if="isPause" @click="handlePlay" :src="sourceUrl + '/bofang.png'"
						class="bofang_icon"></cover-image> -->
					<cover-view class="slide-action-wrap">
						<!-- <cover-view class="user-avatar">
							<cover-image :src="user.avatar" class="full" mode="aspectFill" />
						</cover-view> -->
						<cover-view class="action-item" @click="likeHandle">
							<cover-image
								:src="like ? '../static/icon/thumbs-up-active.png' : '../static/icon/thumbs-up.png'"
								class="good-icon"></cover-image>
							<cover-view class="action-number">{{ likeNum }}</cover-view>
						</cover-view>
						<cover-view class="action-item" @click="collectHandle">
							<cover-image
								:src=" collect ? '../static/icon/collect-active.png' : '../static/icon/collect.png'"
								class="good-icon"></cover-image>
							<cover-view class="action-number">{{ collectNum }}</cover-view>
						</cover-view>
						<cover-view class="action-item">
							<button open-type="share" class="share-btn">
								<cover-image src="../static/share.png" class="good-icon"></cover-image>
							</button>
						</cover-view>
					</cover-view>
				</view>
			</view>
		</view>
		<view class="fixed-footer">
			<view class="user-desc">
				<view class="user_avatar">
					<image :src="user.avatar" class="full" mode="aspectFill"></image>
				</view>
				<view class="user-name">
					<text>{{ user.nickname }}</text>
				</view>
				<view class="create-time">
					<text>{{ createTime }}</text>
				</view>
			</view>
			<view class="desc-wrap">
				<text>{{ desc }}</text>
			</view>
		</view>
		<!-- <view v-if="false" class="fixed-progress">
			<view class="progress-inner" :style="{'--width':progress}"></view>
		</view> -->
	</view>
</template>

<script>
	import {
		mapGetters,
		mapActions
	} from 'vuex'
	import request from '@/utils/request.js'
	import {
		toast,
		patchZero
	} from '@/utils/tool.js'
	export default {
		onShareTimeline() {
			const {
				desc: title,
				id,
			} = this
			return {
				title,
				// imageUrl: image,
				query: `id=${id}`
			}
		},
		onShareAppMessage() {
			const {
				desc: title,
				id,
			} = this
			return {
				title,
				path: '/pages-bundle/video-detail/video-detail?id=' + id,
				// imageUrl: image
			}
		},
		onLoad(ev) {
			this.openWxShare()
			if (!ev) {
				return toast({
					title: '参数错误!'
				}, () => {
					uni.navigateBack()
				})
			}
			if (ev.hasOwnProperty('event')) {
				const {
					event
				} = ev
				this.event = !!event
			}
			const {
				id
			} = ev
			this.initData(id)
			this.id = id
		},
		onShow() {
			if (!this.hasInit) return
			this.initData(this.id)
			this.videoRef.play()
		},
		onReady() {
			this.videoRef = uni.createVideoContext('videoRef', this)
		},
		methods: {
			...mapActions(['getUserinfo']),
			handlePlay() {
				this.videoRef.play()
				this.isPause = false
			},

			handleTest() {
				this.videoRef.requestFullScreen(90)
				return
			},
			handleVideoClick() {
				if (!this.videoRef) return
				const {
					isPause
				} = this
				if (isPause) {
					return
				}
				this.isPause = true
				this.videoRef.pause()
				console.log('videoClick');
			},
			backHandle() {
				const pages = getCurrentPages()
				if (pages.length === 1) return uni.switchTab({
					url: '/pages/index/index'
				})
				uni.navigateBack()
			},
			toComment() {
				this.videoRef.pause()
				uni.navigateTo({
					url: '/pages/article-comment/article-comment?id=' + this.id
				})
			},
			async subscribeAuthor() {
				const {
					userId,
					user: {
						subscribe
					}
				} = this
				const resp = await this.request({
					url: "/author/subscribe",
					data: {
						author_id: userId
					},
				})
				const {
					code,
					msg
				} = resp
				toast({
					title: msg
				})
				if (code !== 1) return
				this.user.subscribe = !subscribe
				console.log(resp, 'resp');
			},
			async likeHandle() {
				const {
					like,
					id
				} = this
				console.log(this.likeNum, 'like');
				const url = like ? '/article/cancelLike' : '/article/addLike'
				const resp = await request({
					url,
					data: {
						id
					},
				})
				const {
					code,
					msg
				} = resp
				let title = msg
				if (code === 1) {
					title = like ? '取消点赞成功' : '点赞成功'
					this.likeNum += like ? -1 : 1
					this.like = !like
				}
				toast({
					title
				})
			},
			async collectHandle() {

				const {
					collect,
					id
				} = this
				const url = collect ? '/article/cancelCollect' : '/article/addCollect'
				const resp = await request({
					url,
					data: {
						id
					},
				})
				const {
					code,
					msg
				} = resp
				let title = msg
				if (code === 1) {
					title = collect ? '取消收藏成功' : '收藏成功'
					this.collectNum += collect ? -1 : 1
					this.collect = !collect
				}
				toast({
					title
				})
				if (this.event) {
					uni.$emit('collectChange', {
						type: 2
					})
				}
			},
			formatTime(time) {
				const date = new Date(time)
				const [month, day, hour, minute] = [date.getMonth() + 1, date.getDate(), date.getHours(), date
					.getMinutes()
				]
				return `${patchZero(month)}-${patchZero(day)} ${patchZero(hour)}:${patchZero(minute)}`
			},
			videoLoadEnd(ev) {
				this.videoRef = uni.createVideoContext('videoRef')
				this.videoRef.play()
				console.log(ev, '加载完成');
			},
			async initData(id) {
				const resp = await request({
					url: "/article/detail",
					data: {
						id
					},
					method: 'GET'
				})
				const {
					code,
					data,
					msg
				} = resp
				console.log(data, 'data');
				if (code !== 1) return toast({
					title: msg
				}, () => {
					uni.navigateBack()
				})
				const {
					video,
					create_time,
					desc,
					collect,
					collect_num,
					like,
					like_num,
					author_img,
					author,
				} = data
				if (!this.hasInit) {
					this.videoPath = video
					this.user = {
						avatar: author_img,
						nickname: author
					}
					this.createTime = this.formatTime(create_time)
					this.desc = desc
					this.hasInit = true
				}
				this.collectNum = collect_num
				this.likeNum = like_num
				this.like = like
				this.collect = collect
			},
			onTimeUpdate(ev) {
				const {
					currentTime,
					duration
				} = ev.detail
				const pix = (currentTime / duration).toFixed(2)
				this.progress = Math.ceil((pix * 750)) + 'rpx'
			}
		},
		created() {
			// #ifdef APP-PLUS
			this.isMuted = false
			// #endif
			// #ifdef MP-WEIXIN
			this.isMuted = false
			// #endif
			this.getUserinfo()
		},
		data() {
			return {
				event: false,
				loading: true,
				isMuted: true,
				progress: '10%',
				id: '',
				user: {
					avatar: '',
					nickname: '',
				},
				videoPath: '',
				createTime: '',
				desc: '',
				collectNum: 0,
				commentNum: 0,
				likeNum: 0,
				like: false,
				collect: false,
				isSelf: false,
				show: true,
				hasInit: false,
				isPause: false, // 视频是否暂停
			};
		},
		computed: {
			...mapGetters(['userInfo']),
		},
	}
</script>

<style lang="scss" scoped>
	.video-detail-wrapper {
		--footer-height: 160rpx;
		--header-height: 88rpx;
		background: #000000;
		padding: var(--padding) 0 calc(env(safe-area-inset-bottom) + 16rpx);

		.bofang_icon {
			position: absolute;
			left: 50%;
			top: 50%;
			transform: translate(-50%, -50%);
			width: 88rpx;
			height: 88rpx;
			z-index: 101;
		}

		.video-target {
			z-index: 100;
		}

		.slide-action-wrap {
			position: absolute;
			right: 32rpx;
			bottom: 32rpx;
			z-index: 2;

			.action-item {
				display: flex;
				flex-flow: column;
				align-items: center;
				margin-top: 48rpx;

				.share-btn {
					background: none;
					border: none;
				}

				.action-number {
					height: 44rpx;
					font-size: 28rpx;
					color: #FFFFFF;
					margin-top: 8rpx;
					line-height: 44rpx;
				}

				.good-icon {
					width: 48rpx;
					height: 48rpx;
				}

			}

			.user-avatar {
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;
				overflow: hidden;
			}
		}

		.fixed-progress {
			width: 100%;
			height: 2rpx;
			background-color: #4A4A4A;

			.progress-inner {
				width: var(--width);
				height: 2rpx;
				background: linear-gradient(135deg, #8BDCFF 0%, #E4FF68 100%);
			}
		}

		.fixed-footer {
			width: 580rpx;
			padding: 0 32rpx 24rpx;
			box-sizing: border-box;
			margin-top: 32rpx;

			.desc-wrap {
				font-size: 24rpx;
				color: #FFFFFF;
				line-height: 40rpx;
				margin-top: 24rpx;
				word-break: break-all;
			}

			.user-desc {
				display: flex;
				align-items: center;
				color: #FFFFFF;

				.user_avatar {
					width: 64rpx;
					height: 64rpx;
					border-radius: 50%;
					overflow: hidden;
					margin-right: 24rpx;
				}

				.user-name {
					height: 44rpx;
					font-size: 28rpx;
					font-weight: bold;
					line-height: 44rpx;
				}

				.create-time {
					font-size: 26rpx;
					margin-left: 16rpx;
				}
			}
		}

		.fixed-header {
			width: 100%;
			height: var(--header-height);
			display: flex;
			align-items: center;
			padding: 0 32rpx;

			.chevron-icon {
				width: 48rpx;
				height: 48rpx;
			}
		}
	}
</style>